﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace GuildofHeroes
{
    class Skeleton
    {
        //const int MOVE_UP = -2;
        //const int MOVE_DOWN = 2;
        //const int MOVE_LEFT = -2;
        //const int MOVE_REIGHT = 2;

        private bool _Update = true;
        private float _AttackRange = 100000;
        private int _Health = 100;
        private int _BaseDamage = 3;
        private int _BaseExperience = 10;
        private int _BaseGold = 5;
        float scale = 3f;
        private AnimatedSprite m_Char = new AnimatedSprite();
        private Vector2 _Velocity;
        Vector2 direction; //always normalise
        Vector2 skellyPos;
        private float _SkelSpeed = 0.5f;
        private Vector2 _BaseVelocity;
        //private int _BaseSpeed = 3;
        private float _AttackDelay = 0f;

        public Skeleton(Texture2D pTexture2D, Vector2 pVelocity, int pPosX, int pPosY, int pNumFrames, int pFrameWidth, int pFrameHeight,
            int pHorizontalSpace, int pVerticalSpace, int pFramesPerSec, float pScale)
        {
            m_Char.LoadSprite(pTexture2D, pPosX, pPosY, pNumFrames, pFrameWidth, pFrameHeight, pHorizontalSpace, pVerticalSpace, pFramesPerSec, pScale);
            _Velocity = pVelocity;
            _BaseVelocity = pVelocity;
        }
       
        public void Update(GameTime pGameTime, Vector2 pPlayerPos, Vector2 pHomePos)
        {
            if (_AttackDelay < 0)
                _AttackDelay = 0.5f;
            if (_Update)
            {
                if (inRange(pPlayerPos))
                {
                    skellyPos.X = m_Char.X;
                    skellyPos.Y = m_Char.Y;

                    direction = Vector2.Subtract(pPlayerPos, skellyPos);
                    float hyp = (float)Math.Sqrt(direction.X * direction.X + direction.Y * direction.Y);

                    direction.X /= hyp;
                    direction.Y /= hyp;

                    Vector2 newPos = new Vector2(direction.X, direction.Y);

                    newPos.X += direction.X * _SkelSpeed;
                    newPos.Y += direction.Y * _SkelSpeed;

                    direction = Vector2.Normalize(Vector2.Subtract(skellyPos, pPlayerPos));
                    pPlayerPos = Vector2.Multiply(Vector2.Add(pPlayerPos, direction), 0.01f);

                    m_Char.UpdatePos(newPos);
                }

                else
                {
                    skellyPos.X = m_Char.X;
                    skellyPos.Y = m_Char.Y;

                    direction = Vector2.Subtract(pHomePos, skellyPos);
                    float hyp = (float)Math.Sqrt(direction.X * direction.X + direction.Y * direction.Y);

                    direction.X /= hyp;
                    direction.Y /= hyp;

                    Vector2 newPos = new Vector2(direction.X, direction.Y);

                    newPos.X += direction.X * _SkelSpeed;
                    newPos.Y += direction.Y * _SkelSpeed;

                    direction = Vector2.Normalize(Vector2.Subtract(skellyPos, pHomePos));
                    pHomePos = Vector2.Multiply(Vector2.Add(pHomePos, direction), 0.01f);

                    m_Char.UpdatePos(newPos);
                }
            }
            _AttackDelay -= (float)pGameTime.ElapsedGameTime.TotalSeconds;
        }

        public void Draw(SpriteBatch pSpriteBatch)
        {
            m_Char.Draw(pSpriteBatch);
        }

        public bool inRange(Vector2 pPosition)
        {
            Vector2 Distance = Vector2.Subtract(new Vector2(this.m_Char.X, this.m_Char.Y), pPosition);
            float distanceSquared = Distance.X * Distance.X + Distance.Y * Distance.Y;

            if (distanceSquared <= _AttackRange)
                return true;

            return false;
        }

        public bool isAlive()
        {
           if (_Health > 0)
               return true;

            return false;
        }

        public void Stop()
        {
            _Update = false;
        }

        public void Start()
        {
            _Update = true;
        }

        public bool isMoving()
        {
            return _Update;
        }

        public void UpdateHealth(bool pDamage, int pAmount)
        {
            if (pDamage)
                _Health -= pAmount;
            if (!pDamage)
                _Health += pAmount;
            if (_Health > 10)
                _Health = 10;
            if (_Health <= 0)
                _Health = 0;
        }

        public Rectangle GetRectangle()
        {
            return m_Char.getRectangle();
        }

        public int GetDamage()
        {
            if (_AttackDelay < 0)
                return _BaseDamage;
            else
                return 0;
        }

        public float GetX()
        {
            return m_Char.X;
        }

        public float GetY()
        {
            return m_Char.Y;
        }

        public int GetWidth()
        {
            return m_Char.GetWidth();
        }

        public int GetGold()
        {
            return _BaseGold;
        }

        public int GetExp()
        {
            return _BaseExperience;
        }
    }
}
